package br.com.unip.visao;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;

import br.com.unip.modelo.Funcionario;
import br.com.unip.persistencia.FuncionarioDao;

public class AppFuncionarioNovoGui extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JDialog dialogFunc;
	private JPanel panelFunc;
	private JLabel labelFunNome, labelFunDataNasc, labelFunSalario,
			labelFunNumDepto, labelFunCodSup;
	private JTextField textFuncNome, textFunSalario, textFunNumDepto,
			textFunCodSup;
	private JFormattedTextField textFunFormatDate;
	private JButton btnFunSalvar, btnFunCancelar;

	public AppFuncionarioNovoGui() {
		super("Projeto UNIP - SWING - ALPOO - Novo funcionário");

		labelFunNome = new JLabel("Nome:");
		labelFunDataNasc = new JLabel("Data de nascimento (ex. 1982-12-31):");
		labelFunSalario = new JLabel("Salário:");
		labelFunNumDepto = new JLabel("Número do departamento:");
		labelFunCodSup = new JLabel("Código do supervisor:");

		textFuncNome = new JTextField(50);
		textFunSalario = new JTextField(50);
		textFunNumDepto = new JTextField(50);
		textFunCodSup = new JTextField(50);

		textFunFormatDate = new JFormattedTextField();

		try {
			textFunFormatDate.setFormatterFactory(new DefaultFormatterFactory(
					new MaskFormatter("####-##-##")));
		} catch (Exception e) {
			e.printStackTrace();
		}

		btnFunSalvar = new JButton("Salvar");
		btnFunCancelar = new JButton("Cancelar");

		btnFunSalvar.addActionListener(this);
		btnFunCancelar.addActionListener(this);
		
		panelFunc = new JPanel();
		panelFunc.setLayout(new GridLayout(6, 2));
		panelFunc.setBorder(BorderFactory.createTitledBorder("Dados"));
		panelFunc.add(labelFunNome);
		panelFunc.add(textFuncNome);
		panelFunc.add(labelFunDataNasc);
		panelFunc.add(textFunFormatDate);
		panelFunc.add(labelFunSalario);
		panelFunc.add(textFunSalario);
		panelFunc.add(labelFunNumDepto);
		panelFunc.add(textFunNumDepto);
		panelFunc.add(labelFunCodSup);
		panelFunc.add(textFunCodSup);
		panelFunc.add(btnFunSalvar);
		panelFunc.add(btnFunCancelar);
		
		dialogFunc = new JDialog();
		dialogFunc.add(panelFunc);
		
		dialogFunc.setResizable(false);
		dialogFunc.setSize(650, 200);
		dialogFunc.setLocationRelativeTo(null);
		dialogFunc.setModal(true);
		dialogFunc.setVisible(true);
	}

	public void actionPerformed(ActionEvent ae) {
		Object evento = ae.getSource();
		if (evento == btnFunSalvar) {
			System.out.println("Ação => Salvando");
			try {
				Funcionario f = new Funcionario();
				FuncionarioDao fd = new FuncionarioDao();
				f.setNome(textFuncNome.getText());
				f.setDataNascimento(Date.valueOf(textFunFormatDate.getText()));
				f.setSalario(Float.parseFloat(textFunSalario.getText()));
				f.setNumeroDepartamento(Integer.parseInt(textFunNumDepto
						.getText()));
				f.setCodigoSupervisor(Integer.parseInt(textFunCodSup.getText()));
				fd.gravar(f);
				textFuncNome.setText("");
				textFunFormatDate.setText("");
				textFunSalario.setText("");
				textFunNumDepto.setText("");
				textFunCodSup.setText("");
				textFuncNome.requestFocus();
				JOptionPane.showMessageDialog(null,
						"Registro salvo com sucesso!", "Sucesso",
						JOptionPane.INFORMATION_MESSAGE);
				
				// Gambiarra que tem que ser arrumada! o.O (sem net para pesquisar)
				AppListGui.paneLista.setSelectedIndex(1);
				AppListGui.paneLista.setSelectedIndex(0);
			} catch (Exception e) {
				JOptionPane.showMessageDialog(null, "Erro : " + e.getMessage(),
						"Atenção!", JOptionPane.ERROR_MESSAGE);
			}
		}
		if (evento == btnFunCancelar) {
			System.out.println("Ação => Cancelando");
			dialogFunc.dispose();
		}
	}
}
